package com.zis.system.server;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.RoleDao;
import com.zis.system.inter.IRoleInter;
import com.zis.system.model.Role;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/4/9
 */
@Service
@Transactional("txManager")
public class RoleInterImpl implements IRoleInter {

    @Autowired
    private RoleDao roleDao;

    @Override
    public ResponseEntity insert(Role model) throws Exception {
        int num = roleDao.getSQLManager().insert(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity update(Role model) throws Exception {
        int num = roleDao.getSQLManager().updateTemplateById(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        int num = roleDao.getSQLManager().deleteById(Role.class, id);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        return null;
    }

    @Override
    public PageQuery<Role> findByPage(SearchParams params) throws Exception {
        PageQuery<Role> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        roleDao.getSQLManager().pageQuery("system.role.query", Role.class, query);
        return query;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        return null;
    }

    /**
     * 查询全部转为JSON
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity findAllToJSON() throws Exception {
        List<Role> roleList = roleDao.all();
        if(roleList != null && roleList.size() > 0){
            JSONArray roleArray = new JSONArray();
            for (Role role : roleList) {
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("key", role.getRoleId());
                jsonObj.put("label", role.getRoleName());
                jsonObj.put("disabled", role.getStatus() == 1 ? false : true);
                roleArray.add(jsonObj);
            }
            return ResponseEntity.success(roleArray);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }
}
